<?php

// +----------------------------------------------------------------------
// | 订单 api控制器
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

/**
 * 订单 api控制器
 * @author fancsen
 */


namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\validate\OrderPlace;
use app\api\service\Order as OrderService;
use app\api\model\Order as OrderModel;
use app\api\service\Token as TokenService;
use app\api\validate\PagingParameter;
use app\api\validate\IDMustBePostiveInt;

class Order extends BaseController
{   
    

    //前置成员变量
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'placeOrder'],
        'checkPrimaryScope'=> ['only' => 'getOrdersByUser,getOrderDetail'],
    ];

    /**
    * 用户下单接口
    * @http post
    * @return array
    */
    public function placeOrder()
    {
        (new OrderPlace())->goCheck();

        $products = input('post.products/a');
        
        $uid = TokenService::getCurrentTokenVar('uid');

        $order = ( new OrderService)->place($uid,$products);

        return json($order);
    }
    /**
    * 获取用户订单分页数据
    * @http get
    * @param int $page 当前页码 
    * @param int $size 每页显示记录数 
    * @return array 
    */
    public function getOrdersByUser($page=1,$size=15)
    {
        (new PagingParameter())->goCheck();

        $uid = TokenService::getCurrentTokenVar('uid');

        $orders = OrderModel::getSummaryByUser($uid,$page,$size);
        
        if($orders->isEmpty()){
            return json([
                'current_page' => $orders->currentPage(),
                'data' => []
            ]);
        }

        $data = $orders->hidden(['snap_items', 'snap_address','prepay_id'])
            ->toArray();
        
        return json([
            'current_page' => $orders->currentPage(),
            'data' => $data
        ]);
    }
    /**
    * 获取单个订单详情
    * @http get
    * @param int 订单id号 
    * @return array
    */
    public function getOrderDetail($id=null)
    {
        (new IDMustBePostiveInt())->goCheck();

        $order = OrderModel::where('id','=',$id)->find();

        if(!$order){
            throw new ApiException("订单不存在，请查id", 404,80003);
            
        }
        $order->hidden(['prepay_id']);
        
        return json($order);
    }
    /**
     * 获取全部订单简要信息（分页）
     * @param int $page
     * @param int $size
     * @return array
     * @throws \app\lib\exception\ParameterException
     */
    public function getSummary($page=1, $size = 20){
        (new PagingParameter())->goCheck();
        $pagingOrders = OrderModel::getSummaryByPage($page, $size);
        if ($pagingOrders->isEmpty())
        {
            return json([
                'current_page' => $pagingOrders->currentPage(),
                'data' => []
            ]);
        }
        $data = $pagingOrders->hidden(['snap_items', 'snap_address'])
            ->toArray();
        return json([
            'current_page' => $pagingOrders->currentPage(),
            'data' => $data,
        ]);
    }

    /**
    * 发货接口
    * @http put
    * @param int 订单id 
    * @return string 发送模版消息
    */
    public function delivery($id=null)
    {
        (new IDMustBePostiveInt())->goCheck();
        //订单发货
        $orderService = new OrderService();
        $result = $orderService->delivery($id);

        if($result){
            //发货成功
            return show(config('api.success_status'),'ok',[],201);
        }
    }
}